package ylyhappy.cn.ideavim.ui.actionpreview;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.card.MaterialCardView;

import ylyhappy.cn.ideavim.databinding.SearchFragmentBinding;
import ylyhappy.cn.ideavim.ui.IdeaVimBuilderApplication;

public class SearchListFragment extends Fragment {

    private SearchFragmentBinding binding;

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = SearchFragmentBinding.inflate(inflater, container, false);
        initActionRecycler();
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding.actionRecyclerList.setAdapter(null);
        binding = null;
    }

    /**
     * Building the action View
     */
    private void initActionRecycler() {
        Context context = binding.getRoot().getContext();
        binding.actionRecyclerList.setLayoutManager(new LinearLayoutManager(context));
        ActionListAdapter actionListAdapter = new ActionListAdapter(IdeaVimBuilderApplication.actions.subList(1, 20), context);
        binding.actionRecyclerList.setAdapter(actionListAdapter);
        actionListAdapter.setmOnItemClickListener(new ActionListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                MaterialCardView container = (MaterialCardView) view;
                Toast.makeText(context, "CRAD CLICK" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

}